<!--
 * @Author: your name
 * @Date: 2021-02-24 16:21:28
 * @LastEditTime: 2021-02-24 20:43:49
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \mypro\lbt.html
-->

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        div {
            width: 600px;
            height: 400px;
            border: 2px solid blue;
            margin: 100px auto;
            position: relative;
            overflow: hidden;
        }

        ul {
            list-style: none;
            height: 400px;
            position: absolute;
            left: -600px;
            top: 0;
        }

        li {
            width: 600px;
            height: 400px;
            float: left;
            text-align: center;
            font-size: 50px;
            font-weight: bold;
            line-height: 400px;
        }
    </style>
</head>

<body>
    <div>
        <ul>
            <li style="background: cyan;">5</li>
            <li style="background: red;">1</li>
            <li style="background: orange;">2</li>
            <li style="background: gold;">3</li>
            <li style="background: green;">4</li>
            <li style="background: cyan;">5</li>
            <li style="background: red;">1</li>
        </ul>
    </div>
    <script src="./tool.js"></script>
    <script>
        // 获取元素
        const vs = document.querySelector('div');
        const ul = document.querySelector('ul');
        const lis = document.querySelectorAll('li');
        //console.log(lis.length)

        // 声明变量
        let liWidth = lis[0].offsetWidth;
        // 设置ul的宽度 = li的宽度 * li的个数
        getCss(ul, 'width', liWidth * lis.length + 'px');
        
        let indexOne = 1;
        let indexTwo = 2;
        let timer = null;
        // 封装轮播效果
        function dh() {
            animate(ul, { left: -indexTwo * liWidth }, 500, () => {
                if (indexOne === 6) {
                    indexOne = 1;
                    indexTwo = 2;
                    getCss(ul, 'left', -indexOne * liWidth + 'px')
                }
                if (indexOne === 0) {
                    indexOne = 5;
                    indexTwo = 6;
                    getCss(ul, 'left', -indexOne * liWidth + 'px')
                }
            })
            indexOne = indexTwo;
            indexTwo++;
            //console.log(indexOne, indexTwo)
        }
        timer = setInterval(dh, 1000)
    </script>

</body>

</html>